#!/usr/bin/env python3

from sanic import Sanic
from sanic.response import text

import wx_message.receive as wx_msg_receive
from wx_message.checking import IsComeFromWx
from message_handler import HandleMessage

from __private__ import *

app = Sanic(__name__)


@app.route('/wx', methods=['POST'])
async def handler(request):
    if not IsComeFromWx(WX_MSG_TOKEN, request.args):
        return text('fuck you')

    xml_str = request.body.decode('utf-8')
    try:
        receive_msg = wx_msg_receive.FromXml(xml_str)
    except:
        return text('success')

    reply_msg = HandleMessage(receive_msg)
    return text(reply_msg)


if __name__ == '__main__':
    app.run(host = '0.0.0.0', port = 80, debug = True, workers = 4)
